package com.zhangchuchu.neohealth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.Comments;
import com.zhangchuchu.neohealth.entity.DTO.CommentsDTO;
import com.zhangchuchu.neohealth.service.CommentsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import static com.zhangchuchu.neohealth.common.R.success;

/**
 * 评论表(Comments)表控制层
 *
 * @author makejava
 * @since 2025-03-29 20:34:32
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    /**
     * 服务对象
     */
    @Resource
    private CommentsService commentsService;

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param comments 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Comments> page, Comments comments) {
        return success(this.commentsService.page(page, new QueryWrapper<>(comments)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.commentsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param comments 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Comments comments) {
        return success(this.commentsService.save(comments));
    }

    /**
     * 修改数据
     *
     * @param comments 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Comments comments) {
        return success(this.commentsService.updateById(comments));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.commentsService.removeByIds(idList));
    }
    @GetMapping("/main/{communityId}")
    public R<List<CommentsDTO>> getMainComments(@PathVariable Long communityId) {
        return R.success(commentsService.getMainCommentsByCommunityId(communityId));
    }
    @GetMapping("/sub/{communityId}/{commentId}")
    public R<List<CommentsDTO>> getSubComments(@PathVariable Long communityId,@PathVariable Long commentId) {
        return R.success(commentsService.getSubCommentsByCommunityId(communityId,commentId));
    }
    @GetMapping("/Onesub/{communityId}/{commentId}")
    public R<List<CommentsDTO>> getOneSubComments(@PathVariable Long communityId,@PathVariable Long commentId) {
        return R.success(commentsService.getOneSubCommentsByCommunityId(communityId,commentId));
    }
}

